package com.atguigu.rabbit.producer.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitmqDirectExchangeConfiguration {

    // 声明交换机
    @Bean
    public Exchange directExchange() {
        Exchange directExchange = ExchangeBuilder.directExchange("direct_exchange").durable(true).build();
        return directExchange;
    }

    // // 声明队列
    // @Bean
    // public Queue directQueue01() {
    //     // .durable 创建持久化队列
    //     // .nonDurable 创建非持久化队列
    //     // .ttl 设置队列中消息的存储时间，在这个消息队列中存储的消息在设定的时间内并且没有消费就会被自动清除
    //     Queue queue = QueueBuilder
    //             .durable("direct_queue_01")
    //             // .ttl(5000)
    //             .build();
    //     return queue;
    // }

    // 声明队列
    @Bean
    public Queue directQueue02() {
        Queue queue = QueueBuilder.durable("direct_queue_02").build();
        return queue;
    }

    // 声明队列和交换机的绑定信息
    @Bean
    public Binding directQueue01Binding() {
        Binding binding = BindingBuilder.bind(directQueue01()).to(directExchange())
                .with("error").noargs();
        return binding;
    }

    @Bean
    public Binding bindingQueue02BindingErrorRoutingKey() {
        Binding binding = BindingBuilder.bind(directQueue02()).to(directExchange())
                .with("error").noargs();
        return binding;
    }

    @Bean
    public Binding bindingQueue02BindingInfoRoutingKey() {
        Binding binding = BindingBuilder.bind(directQueue02()).to(directExchange())
                .with("info").noargs();
        return binding;
    }

    @Bean
    public Binding bindingQueue03BindingInfoRoutingKey() {
        Binding binding = BindingBuilder.bind(directQueue02()).to(directExchange())
                .with("warning").noargs();
        return binding;
    }

    /// ///////////////////// 死信队列配置 ///////////////////////

    // 声明死信交换机
    @Bean
    public Exchange dlxExchange() {
        Exchange dlx = ExchangeBuilder.directExchange("dlx_exchange").durable(true).build();
        return dlx;
    }

    // 声明死信队列
    @Bean
    public Queue dlxQueue() {
        Queue queue = QueueBuilder.durable("dlx_queue").build();
        return queue;
    }

    // 声明死信交换机和死信队列绑定信息
    @Bean
    public Binding dlxQueueBinding() {
        Binding binding = BindingBuilder.bind(dlxQueue()).to(dlxExchange())
                .with("dlx").noargs();
        return binding;
    }


    // // 声明队列，并且绑定死信交换机
    // @Bean
    // public Queue directQueue01() {
    //     Queue queue = QueueBuilder.durable("direct_queue_01")
    //             .maxLength(10)
    //             .deadLetterExchange("dlx_exchange")
    //             .deadLetterRoutingKey("dlx")
    //             .build();
    //     return queue ;
    // }

    /// ///////////////////// 延迟队列配置 ///////////////////////

    // 声明队列，设置存储信息的存活时间，并且绑定死信交换机
    @Bean
    public Queue directQueue01() {
        Queue queue = QueueBuilder.durable("direct_queue_01")
                .ttl(10000)
                .deadLetterExchange("dlx_exchange")
                .deadLetterRoutingKey("dlx")
                .build();
        return queue;
    }
}